package com.aierkeji.call.cms.service;

import java.util.HashMap;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.aierkeji.call.cms.contants.Contants;
import com.aierkeji.call.cms.dao.jpa.AkjCallClueDao;
import com.aierkeji.call.cms.dao.jpa.AkjCallClueGroupDao;
import com.aierkeji.call.cms.dao.jpa.AkjCallSystemHistoryDao;
import com.aierkeji.call.cms.entity.AkjCallClueGroup;
import com.aierkeji.call.cms.entity.AkjCallSystemHistory;
import com.aierkeji.call.cms.entity.AkjCallUser;
import com.aierkeji.call.cms.entity.snap.AkjCallUserSnap;
import com.aierkeji.call.cms.util.DateTimeUtils;
import com.aierkeji.call.cms.util.DynamicSpecifications;
import com.aierkeji.call.cms.util.PageUtils;
import com.aierkeji.call.cms.util.ResultMapHelper;
import com.aierkeji.call.cms.util.SearchFilter;
import com.aierkeji.call.cms.util.StringUtils;

@Service
public class AkjCallClueGroupService {
	private final Logger log = LogManager.getLogger(getClass());

	@Autowired
	private AkjCallClueGroupDao akjCallClueGroupDao;
	@Autowired
	private AkjCallClueDao akjCallClueDao;
	@Autowired
	private AkjCallSystemHistoryDao akjCallSystemHistoryDao;

	public Page<AkjCallClueGroup> getClueGroup(Integer pageNo) {
		Subject subject = SecurityUtils.getSubject();
		AkjCallUser akjCallUser = (AkjCallUser) subject.getSession().getAttribute("loginUser");
		Map<String, Object> map = new HashMap<String, Object>();
		if (subject.hasRole(Contants.ROLE_ID_MANAGER)) {
			map.put("JOINEQ_createdBy.id", akjCallUser.getId());
		}
		map.put("EQ_isDelete", false);
		map.put("EQ_isRemove", false);
		Map<String, SearchFilter> filters = SearchFilter.parse(map);
		Specification<AkjCallClueGroup> specs = DynamicSpecifications.bySearchFilter(filters.values(),
				AkjCallClueGroup.class);
		Sort sort = Sort.by(Sort.Order.desc("updatedAt"));
		PageRequest pageRequest = PageUtils.buildPageRequest(pageNo, Contants.PAGE_SIZE, sort);

		return akjCallClueGroupDao.findAll(specs, pageRequest);
	}

	@Transactional
	public Map<String, Object> createCullGroup(Map<String, Object> map) {
		Subject subject = SecurityUtils.getSubject();
		String name = (String) map.get("name");
		if (StringUtils.isEmpty(name)) {
			return ResultMapHelper.nullParamsResult();
		}
		AkjCallUser akjCallUser = (AkjCallUser) subject.getSession().getAttribute("loginUser");
		AkjCallClueGroup akjCallClueGroup = new AkjCallClueGroup();
		akjCallClueGroup.setCreatedAt(DateTimeUtils.getCurrentTime());
		akjCallClueGroup.setCreatedBy(new AkjCallUserSnap(akjCallUser.getId()));
		akjCallClueGroup.setUpdatedAt(DateTimeUtils.getCurrentTime());
		akjCallClueGroup.setUpdatedBy(new AkjCallUserSnap(akjCallUser.getId()));
		akjCallClueGroup.setGroupName(name);
		akjCallClueGroupDao.save(akjCallClueGroup);
		return ResultMapHelper.success();
	}

	@Transactional
	public Map<String, Object> editCullGroup(Map<String, Object> map) {
		Subject subject = SecurityUtils.getSubject();
		String name = (String) map.get("groupName");
		Integer id = (Integer) map.get("id");
		if (StringUtils.checkAnyNull(name, id + "")) {
			return ResultMapHelper.nullParamsResult();
		}
		AkjCallUser akjCallUser = (AkjCallUser) subject.getSession().getAttribute("loginUser");
		AkjCallClueGroup akjCallClueGroup = akjCallClueGroupDao
				.findByCreatedByAndIsDeleteAndId(AkjCallUserSnap.build(akjCallUser), false, Long.parseLong(id + ""));
		if (akjCallClueGroup == null) {
			return ResultMapHelper.nullParamsResult();
		}
		akjCallClueGroup.setUpdatedAt(DateTimeUtils.getCurrentTime());
		akjCallClueGroup.setUpdatedBy(new AkjCallUserSnap(akjCallUser.getId()));
		akjCallClueGroup.setGroupName(name);
		akjCallClueGroupDao.save(akjCallClueGroup);
		return ResultMapHelper.success();
	}

	@Transactional
	public Map<String, Object> deleteCullGroup(Long id, Map<String, Object> request) {
		// TODO Auto-generated method stub
		Subject subject = SecurityUtils.getSubject();
		AkjCallUser akjCallUser = (AkjCallUser) subject.getSession().getAttribute("loginUser");
		AkjCallClueGroup akjCallClueGroup = akjCallClueGroupDao
				.findByCreatedByAndIsDeleteAndId(AkjCallUserSnap.build(akjCallUser), false, Long.parseLong(id + ""));
		if (akjCallClueGroup == null) {
			return ResultMapHelper.nullParamsResult();
		}
		int count = this.akjCallClueGroupDao.deleteClueGroup(id, AkjCallUserSnap.build(akjCallUser),
				DateTimeUtils.getCurrentTime());
		if (count > 0) {

//			int deleteClueCount = akjCallClueDao.deleteByClueGroup(akjCallClueGroup);
//			String content = akjCallUser.getName() + "于,"
//					+ DateTimeUtils.converTimestampToString(DateTimeUtils.getCurrentTime()) + "删除了线索分组"
//					+ akjCallClueGroup.getGroupName() + "同时删除了线索" + deleteClueCount + "条";
//			AkjCallSystemHistory akjCallSystemHistory = new AkjCallSystemHistory(content, akjCallUser.getId());
//			akjCallSystemHistoryDao.save(akjCallSystemHistory);
			return ResultMapHelper.success();

		}
		return ResultMapHelper.failure("刪除失敗！");
	}
}
